package com.rc.saas.tenant.service.tenant;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rc.saas.tenant.base.BaseSearch;
import com.rc.saas.tenant.mapper.enterprise.EnterpriseMemberApplyMapper;
import com.rc.saas.tenant.mapper.operator.RcOperatorMapper;
import com.rc.saas.tenant.mapper.tenant.TenantRoleMapper;
import com.rc.saas.tenant.mapper.tenant.TenantRoleMenuMapper;
import com.rc.saas.tenant.mapper.tenant.TenantUserMapper;
import com.rc.saas.tenant.model.operator.RcOperator;
import com.rc.saas.tenant.model.operator.RcOperatorExample;
import com.rc.saas.tenant.model.tenant.TenantRole;
import com.rc.saas.tenant.model.tenant.TenantRoleExample;
import com.rc.saas.tenant.model.tenant.TenantRoleMenu;
import com.rc.saas.tenant.model.tenant.TenantUser;
import com.rc.saas.tenant.search.tenant.TenantRoleSearch;
import com.rc.saas.tenant.service.operator.RcOperatorService;
import com.rc.saas.tenant.vo.tenant.TenantUserRoleVo;
import com.rc.saas.tenant.vo.tenant.TenantUserVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author wangzhengwei
 * @create 2021/7/27
 */
@Service
public class TenantRoleServiceImpl implements TenantRoleService {

    @Autowired
    private TenantRoleMapper tenantRoleMapper;

    @Autowired
    private TenantRoleMenuMapper tenantRoleMenuMapper;

    @Autowired
    private TenantUserMapper tenantUserMapper;

    @Autowired
    private RcOperatorMapper rcOperatorMapper;

    @Autowired
    private EnterpriseMemberApplyMapper enterpriseMemberApplyMapper;



    @Override
    public TenantRoleMapper getMapper() {
        return tenantRoleMapper;
    }

    @Override
    public PageInfo<TenantRole> listPageTenantRole(TenantRoleSearch tenantRoleSearch) {
        PageHelper.startPage(tenantRoleSearch.getOffset(), tenantRoleSearch.getLimit());
        List<TenantRole> list = tenantRoleMapper.listPageTenantRole(tenantRoleSearch);
        return new PageInfo<>(list);
    }

    @Override
    public void createRole(TenantRole role, TenantUser user) {
        tenantRoleMapper.insertReturnId(role, user);

        //新增授权
        List<Long> menuIds = role.getMenuIds();
        for (Long menuId : menuIds) {
            TenantRoleMenu menu = new TenantRoleMenu();
            menu.setTenantRoleId(role.getTenantRoleId());
            menu.setTenantMenuId(menuId);
            tenantRoleMenuMapper.insertSelective(menu);
        }
    }

    @Override
    public void updateRole(TenantRole role) {
        updateByPrimaryKeySelective(role);

        //先删除原授权
        tenantRoleMenuMapper.deleteByRole(role.getTenantRoleId());

        //新增授权
        List<Long> menuIds = role.getMenuIds();
        for (Long menuId : menuIds) {
            TenantRoleMenu menu = new TenantRoleMenu();
            menu.setTenantRoleId(role.getTenantRoleId());
            menu.setTenantMenuId(menuId);
            tenantRoleMenuMapper.insertSelective(menu);
        }
    }

    @Override
    public TenantUserRoleVo getLonginRole(String loginName) {
        int roleType= 0;
        if (StringUtils.isBlank(loginName)){
            return null;
        }
        TenantUserRoleVo roleVo = tenantUserMapper.getLoginRole(loginName);
     /*   roleVo.setRoleType("1");
        roleVo.setDataAuth("011");*/
        if (roleVo==null){
            return null;
        }

        if (StringUtils.equals("0",roleVo.getRoleType())){//超级管理员
            return roleVo;
        }
        switch (roleVo.getRoleType()){
            case "1" :
                roleType = 1;//自定义角色
                dealRole(roleVo);
                break;
            case "2":
                roleType = 2; //加盟商角色
                //加盟商
                roleVo.setOperatorIds(rcOperatorMapper.loadOperatorIds("0",loginName).getOperatorIds());
                break;
            case "3":
                roleType = 3; //企业角色
                //企业
                roleVo.setEnterpriseIds(enterpriseMemberApplyMapper.loadEnterpriseIds("0",loginName).getEnterpriseIds());
                break;
        }
        return roleVo;
    }

    @Override
    public TenantRole selectByRoleType(String roleType) {
        TenantRoleExample example = new TenantRoleExample();
        example.createCriteria().andRoleTypeEqualTo(roleType);
        List<TenantRole> tenantRoles = tenantRoleMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(tenantRoles)){
            return tenantRoles.get(0);
        }
        return null;
    }

    public void dealRole(TenantUserRoleVo roleVo){
        //roleVo.setDataAuth("010");
        if (StringUtils.equals("100",roleVo.getDataAuth())){//单独平台权限
            roleVo.setOperatorIds(rcOperatorMapper.loadOperatorIds("1",null).getOperatorIds());
        }
        if (StringUtils.equals("010",roleVo.getDataAuth())||StringUtils.equals("110",roleVo.getDataAuth())){
            //单独加盟商权限/平台+加盟商
            roleVo.setOperatorIds(rcOperatorMapper.loadOperatorIds(null,null).getOperatorIds());
        }
        if (StringUtils.equals("001",roleVo.getDataAuth())||StringUtils.equals("101",roleVo.getDataAuth())){//单独企业权限
            roleVo.setEnterpriseIds(enterpriseMemberApplyMapper.loadEnterpriseIds(null,null).getEnterpriseIds());
        }
        if (StringUtils.equals("011",roleVo.getDataAuth())){//加盟商+企业
            roleVo.setOperatorIds(rcOperatorMapper.loadOperatorIds(null,null).getOperatorIds());
            roleVo.setEnterpriseIds(enterpriseMemberApplyMapper.loadEnterpriseIds(null,null).getEnterpriseIds());
        }
        /*if (StringUtils.equals("111",roleVo.getDataAuth())){//全部权限不动

        }*/
    }


}
